package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.School;
import cn.wolfcode.crm.query.SchoolQueryObject;
import cn.wolfcode.crm.service.ISchoolService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("school")
public class SchoolController {
    @Autowired
    public ISchoolService schoolService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"school:view","大客户列表"},logical = Logical.OR)
    public String view() {
        return "school/school";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(SchoolQueryObject qo) {
        PageResult query = schoolService.query(qo);
        return query;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<School> selectAll() {

        return schoolService.selectAll();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"school:delete","大客户删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            schoolService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"school:saveOrUpdate","大客户新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(School school){
        try {
            if(school.getId() != null){
                schoolService.updateByPrimaryKey(school);
            }else{
                schoolService.insert(school);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"school:changeState", "签约或取消签约"}, logical = Logical.OR)
    public JSONResult changeState(Long id) {
        try {
            schoolService.changState(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("取消签约成功");
        }
    }

    @RequestMapping("exportXls")
    public void exportXls(SchoolQueryObject qo, HttpServletResponse response) throws IOException {
        response.setHeader("Content-disposition", "attachment;filename=school.xls");
        HSSFWorkbook workbook = schoolService.exportXls(qo);
        workbook.write(response.getOutputStream());
    }

}
